import { defineStore } from 'pinia'
import { recordGetRecordApi, getImageUrlApi } from '@/api/ReviewBusiness/01Project/project'
import { getArrayToHis } from '@/utils/index'
// 定义store
export const docmentReviewStore = defineStore('docmentReviewStore', {
  state: () => {
    return {
      // 数据怎么处理 1 查询数据 2 canvas数据 3 mark数据 4 analysis 
      fileDate: {},
      search: {
        filesId: '',
        pageNumber: 1
      },
      // img数据
      recordList: [],
      imageId: '',
      img_info: [] as any,
      numPages: 0,
      type:'R'
      // mark
      // analysis
    }
  },
  actions: {
    // 获取图片
    getFileImage(search) {
      this.img_info = []
      return new Promise((resolve, reject) => {
        recordGetRecordApi(search).then(async res => {
          if (res.code == 200 && res.data.length > 0) {
            //数据
            console.log(res);
            this.recordList = res.data
            this.numPages = res.data[0].count
            const imagePath = res.data[0].imagePath
            this.imageId = res.data[0].imageId
            const path = imagePath.substring(8, imagePath.length)
            const file = await this.getImageUrl(path) as any
            
            const image = window.webkitURL.createObjectURL(new Blob([file.data], { type: 'image/png' }))
            const arr = getArrayToHis(res.data)
            const item = {
              imageBase64: `${image}`,
              page: this.search.pageNumber,
              fileId: this.search.filesId,
              commentsDTOs: arr,
              records: res.data
            }
            // geiyige
            this.img_info.push({ ...item })
          } else {
            // 没有数据 
            this.recordList = []
            this.numPages = 0
            this.imageId = ''
          }
          //返回 我现在
          resolve(res);
        }).catch(error => {
          reject(error)
        })
      })
    },
    getImageUrl(path) {
      return new Promise((resolve, reject) => {
        getImageUrlApi(path).then((res) => {
          resolve(res)
        }).catch((err) => {
          console.log(err);
          reject(err)

        })
      })
    },

    //  action和mpunted结合的东西
    getFileList(row) {
      this.fileDate = row
      this.search.filesId = row.id
      this.search.pageNumber = 1
    }
  },
  getters: {

  }
})